import { PureComponent } from 'react';
import { getCurrentInstance } from '@tarojs/taro';
import LiveOperater from '@/components/LiveOperater';
import LiveRoom from '@/components/LiveRoom';
import { makeFacade } from "@/facade/roomFacade";
import { makeFacade as makeMsgFacade } from "@/facade/messageFacade";

type IProps = {
}

type IState = {
  roomId: string
}

class Index extends PureComponent<IProps, IState> {
  constructor(props: IProps) {
    super(props);
    this.state = {
      roomId: ""
    }
  }

  componentWillMount() {
    console.log("room页面onload")
    const router = getCurrentInstance().router;

    if (!!router && !!router.params && !!router.params.roomId) {
      //路径必须带roomId
      const thisRoomId = router.params.roomId;
      this.setState({
        roomId: thisRoomId
      })
    }
  }

  componentDidMount() {
    // 保持屏幕常亮
    // Taro.setKeepScreenOn({
    //   keepScreenOn: true
    // })
  }

  // componentDidCatchError () {}

  componentWillUnmount() {
    console.log("销毁room直播间页面");

    const { roomId } = this.state;
    //roomId = !roomId ? this.roomFacade.roomId : roomId;
    this.roomFacade.leaveRoom(roomId);
  }

  componentDidShow() {
    //this.roomFacade.resume()
  }

  componentDidHide() {
    //this.roomFacade.pause()
  }

  onResize(res) {
    // 新的显示区域宽度
    console.log("切换横竖屏后的宽度：", res.size.windowWidth)
    console.log("切换横竖屏后的高度：", res.size.windowHeight)
  }

  //类组件的属性成员定义要放在componentWillUnmount这些方法之后
  roomFacade = makeFacade();

  msgFacade = makeMsgFacade();

  //页面分享
  // onShareAppMessage(res: any) {
  //   const { roomId } = this.state;

  //   if (res.from === 'button') {
  //     // 来自页面内转发按钮
  //     console.log(res.target)
  //   }
  //   return {
  //     title: this.roomFacade.roomName,
  //     path: `pages/liveRoom/LiveDetails/index?roomId=${roomId}`
  //   }
  // }

  render() {
    const { roomId } = this.state;

    return (
      <LiveRoom roomId={roomId}>
        <LiveOperater roomId={roomId}></LiveOperater>
      </LiveRoom>
    )
  }
}

export default Index
